package com.baselibrary.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;

import com.baselibrary.Constants;
import com.baselibrary.R;
import com.baselibrary.permission.PermissionCollection;
import com.baselibrary.permission.PermissionsUtils;
import com.baselibrary.permission.callback.PermissionsCall;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ImageFilter;
import com.baselibrary.utils.ScreenUtils;
import com.baselibrary.utils.VideoDownloadUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.kongzue.dialog.v3.CustomDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片详情浏览
 */
public class LocusImagePageAdapter extends PagerAdapter {


    private List<String> mImgList = new ArrayList<>();
    private final Activity context;
    private final int viewPagerHeight;
    private OnItemClickListener mOnItemClickListener;

    public LocusImagePageAdapter(List<String> mImgList, int pagerMaxHeight, Activity context) {
        this.mImgList = mImgList;
        this.context = context;
        this.viewPagerHeight = pagerMaxHeight;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_locus_img_pager, container, false);
        LinearLayout rlImg = view.findViewById(R.id.ll_locuspage);
        ImageView img = view.findViewById(R.id.iv_locuspage);
        ImageView mIvGround = view.findViewById(R.id.iv_ground);

        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        int imgHeight = Integer.parseInt(ScreenUtils.getVideoHight(mImgList.get(position % mImgList.size())));
        int imgWidth = Integer.parseInt(ScreenUtils.getVideoWidth(mImgList.get(position % mImgList.size())));
        //ScreenUtils.setLocusItemHeight(rlImg, img, imgHeight, imgWidth, viewPagerHeight, context);

        Glide.with(context).load(mImgList.get(position % mImgList.size())).into(img);
        Glide.with(context).asBitmap().load(mImgList.get(position % mImgList.size()))
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        //处理得到模糊效果的图
                        Bitmap blurBitmap = ImageFilter.blurBitmap(context, bitmap, 24);
                        Glide.with(context).load(blurBitmap).into(mIvGround);
                    }
                });
        view.setOnClickListener(v -> {
            if (mOnItemClickListener != null)
                mOnItemClickListener.onItemClick();

        });
        view.setOnLongClickListener(v -> {
            CustomDialog.show((AppCompatActivity) container.getContext(), R.layout.dialog_down_image, new CustomDialog.OnBindView() {
                @Override
                public void onBind(final CustomDialog dialog, View v) {
                    ImageView btnOk = v.findViewById(R.id.btn_ok);
                    btnOk.setOnClickListener(v1 -> {
                        dialog.doDismiss();
                        PermissionsUtils.getInstance().chekPermissions(context, PermissionCollection.FILE.getPermissionList(), new PermissionsCall() {
                            @Override
                            public void permissionsPass() {
                                VideoDownloadUtil.download((Activity) container.getContext(), mImgList.get(position % mImgList.size()), ".jpg");
                            }
                        });
                    });
                }
            });
            return false;
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                ArrayList<String> imgList = new ArrayList<>(mImgList);
                bundle.putStringArrayList(Constants.WIKI_PICTURE_LIST, imgList);
                bundle.putInt(Constants.WIKI_PICTURE_INDEX, position);
                ActivityRouter.startActivity(ActivityRouter.ACTIVITY.BROWSE_MEDIA, bundle);
            }
        });
        container.addView(view, 0);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
//        return mImgList.size() > 1 ? Integer.MAX_VALUE : mImgList.size();
        return mImgList.size();
    }


    public interface OnItemClickListener {
        void onItemClick();
    }

}